﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using XcoAppSpaces.Security.Basic;
using System.Reflection;
using System.IO;

namespace test.XcoAppSpaces.Security.Basic
{
	[TestFixture]
	public class testUserStore
	{
		[Test]
		public void TestLoadSave()
		{
			UserStore store = new UserStore(null);
			store.Save(CreateUsers());

			List<User> users = store.Load();
			Assert.AreEqual(3, users.Count);
		}

		[Test]
		public void TestSaveClear()
		{
			UserStore store = new UserStore(null);
			store.Save(CreateUsers());
			store.Clear();

			List<User> users = store.Load();
			Assert.IsNull(users);
		}

		[Test]
		public void TestLoadSaveWithPath()
		{
			string path = Path.Combine(Path.GetDirectoryName(Assembly.GetAssembly(typeof(UserStore)).Location), "users.xml");
			if (File.Exists(path))
				File.Delete(path);
			UserStore store = new UserStore(path);
			store.Save(CreateUsers());

			Assert.IsTrue(File.Exists(path));

			List<User> users = store.Load();
			Assert.AreEqual(3, users.Count);
		}

		private List<User> CreateUsers()
		{
			List<User> users = new List<User>{
				new User("test1", PasswordUtil.CreatePasswordHash("test1"), "role1", "role2"),
				new User("test2", PasswordUtil.CreatePasswordHash("test2"), "role3"),
				new User("test3", PasswordUtil.CreatePasswordHash("test3"))
			};
			return users;
		}
	}
}
